home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / Python1.4_Source / Objects / tupleobject.c < prev    next >
C/C++ Source or Header  |  1998-06-24  |  10KB  |  468 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Tuple object implementation */
  33.  
  34. #include "allobjects.h"
  35.  
  36. #include "protos/tupleobject_protos.h"
  37.  
  38. #ifndef MAXSAVESIZE
  39. #define MAXSAVESIZE    20
  40. #endif
  41.  
  42. #if MAXSAVESIZE > 0
  43. /* Entries 1 upto MAXSAVESIZE are free lists, entry 0 is the empty
  44.    tuple () of which at most one instance will be allocated.
  45. */
  46. static tupleobject *free_tuples[MAXSAVESIZE];
  47. #endif
  48. #ifdef COUNT_ALLOCS
  49. int fast_tuple_allocs;
  50. int tuple_zero_allocs;
  51. #endif
  52.  
  53. object *
  54. newtupleobject(size)
  55.     register int size;
  56. {
  57.     register int i;
  58.     register tupleobject *op;
  59.     if (size < 0) {
  60.         err_badcall();
  61.         return NULL;
  62.     }
  63. #if MAXSAVESIZE > 0
  64.     if (size == 0 && free_tuples[0]) {
  65.         op = free_tuples[0];
  66.         INCREF(op);
  67. #ifdef COUNT_ALLOCS
  68.         tuple_zero_allocs++;
  69. #endif
  70.         return (object *) op;
  71.     }
  72.     if (0 < size && size < MAXSAVESIZE && (op = free_tuples[size]) != NULL) {
  73.         free_tuples[size] = (tupleobject *) op->ob_item[0];
  74. #ifdef COUNT_ALLOCS
  75.         fast_tuple_allocs++;
  76. #endif
  77.     } else
  78. #endif
  79.     {
  80.         op = (tupleobject *)
  81.             malloc(sizeof(tupleobject) + size * sizeof(object *));
  82.         if (op == NULL)
  83.             return err_nomem();
  84.     }
  85.     op->ob_type = &Tupletype;
  86.     op->ob_size = size;
  87.     for (i = 0; i < size; i++)
  88.         op->ob_item[i] = NULL;
  89.     NEWREF(op);
  90. #if MAXSAVESIZE > 0
  91.     if (size == 0) {
  92.         free_tuples[0] = op;
  93.         INCREF(op);    /* extra INCREF so that this is never freed */
  94.     }
  95. #endif
  96.     return (object *) op;
  97. }
  98.  
  99. int
  100. gettuplesize(op)
  101.     register object *op;
  102. {
  103.     if (!is_tupleobject(op)) {
  104.         err_badcall();
  105.         return -1;
  106.     }
  107.     else
  108.         return ((tupleobject *)op)->ob_size;
  109. }
  110.  
  111. object *
  112. gettupleitem(op, i)
  113.     register object *op;
  114.     register int i;
  115. {
  116.     if (!is_tupleobject(op)) {
  117.         err_badcall();
  118.         return NULL;
  119.     }
  120.     if (i < 0 || i >= ((tupleobject *)op) -> ob_size) {
  121.         err_setstr(IndexError, "tuple index out of range");
  122.         return NULL;
  123.     }
  124.     return ((tupleobject *)op) -> ob_item[i];
  125. }
  126.  
  127. int
  128. settupleitem(op, i, newitem)
  129.     register object *op;
  130.     register int i;
  131.     object *newitem;
  132. {
  133.     register object *olditem;
  134.     register object **p;
  135.     if (!is_tupleobject(op)) {
  136.         XDECREF(newitem);
  137.         err_badcall();
  138.         return -1;
  139.     }
  140.     if (i < 0 || i >= ((tupleobject *)op) -> ob_size) {
  141.         XDECREF(newitem);
  142.         err_setstr(IndexError, "tuple assignment index out of range");
  143.         return -1;
  144.     }
  145.     p = ((tupleobject *)op) -> ob_item + i;
  146.     olditem = *p;
  147.     *p = newitem;
  148.     XDECREF(olditem);
  149.     return 0;
  150. }
  151.  
  152. /* Methods */
  153.  
  154. static void
  155. tupledealloc(op)
  156.     register tupleobject *op;
  157. {
  158.     register int i;
  159.     for (i = 0; i < op->ob_size; i++)
  160.         XDECREF(op->ob_item[i]);
  161. #if MAXSAVESIZE > 0
  162.     if (0 < op->ob_size && op->ob_size < MAXSAVESIZE) {
  163.         op->ob_item[0] = (object *) free_tuples[op->ob_size];
  164.         free_tuples[op->ob_size] = op;
  165.     } else
  166. #endif
  167.         free((ANY *)op);
  168. }
  169.  
  170. static int
  171. tupleprint(op, fp, flags)
  172.     tupleobject *op;
  173.     FILE *fp;
  174.     int flags;
  175. {
  176.     int i;
  177.     fprintf(fp, "(");
  178.     for (i = 0; i < op->ob_size; i++) {
  179.         if (i > 0)
  180.             fprintf(fp, ", ");
  181.         if (printobject(op->ob_item[i], fp, 0) != 0)
  182.             return -1;
  183.     }
  184.     if (op->ob_size == 1)
  185.         fprintf(fp, ",");
  186.     fprintf(fp, ")");
  187.     return 0;
  188. }
  189.  
  190. static object *
  191. tuplerepr(v)
  192.     tupleobject *v;
  193. {
  194.     object *s, *comma;
  195.     int i;
  196.     s = newstringobject("(");
  197.     comma = newstringobject(", ");
  198.     for (i = 0; i < v->ob_size && s != NULL; i++) {
  199.         if (i > 0)
  200.             joinstring(&s, comma);
  201.         joinstring_decref(&s, reprobject(v->ob_item[i]));
  202.     }
  203.     DECREF(comma);
  204.     if (v->ob_size == 1)
  205.         joinstring_decref(&s, newstringobject(","));
  206.     joinstring_decref(&s, newstringobject(")"));
  207.     return s;
  208. }
  209.  
  210. static int
  211. tuplecompare(v, w)
  212.     register tupleobject *v, *w;
  213. {
  214.     register int len =
  215.         (v->ob_size < w->ob_size) ? v->ob_size : w->ob_size;
  216.     register int i;
  217.     for (i = 0; i < len; i++) {
  218.         int cmp = cmpobject(v->ob_item[i], w->ob_item[i]);
  219.         if (cmp != 0)
  220.             return cmp;
  221.     }
  222.     return v->ob_size - w->ob_size;
  223. }
  224.  
  225. static long
  226. tuplehash(v)
  227.     tupleobject *v;
  228. {
  229.     register long x, y;
  230.     register int len = v->ob_size;
  231.     register object **p;
  232.     x = 0x345678L;
  233.     p = v->ob_item;
  234.     while (--len >= 0) {
  235.         y = hashobject(*p++);
  236.         if (y == -1)
  237.             return -1;
  238.         x = (x + x + x) ^ y;
  239.     }
  240.     x ^= v->ob_size;
  241.     if (x == -1)
  242.         x = -2;
  243.     return x;
  244. }
  245.  
  246. static int
  247. tuplelength(a)
  248.     tupleobject *a;
  249. {
  250.     return a->ob_size;
  251. }
  252.  
  253. static object *
  254. tupleitem(a, i)
  255.     register tupleobject *a;
  256.     register int i;
  257. {
  258.     if (i < 0 || i >= a->ob_size) {
  259.         err_setstr(IndexError, "tuple index out of range");
  260.         return NULL;
  261.     }
  262.     INCREF(a->ob_item[i]);
  263.     return a->ob_item[i];
  264. }
  265.  
  266. static object *
  267. tupleslice(a, ilow, ihigh)
  268.     register tupleobject *a;
  269.     register int ilow, ihigh;
  270. {
  271.     register tupleobject *np;
  272.     register int i;
  273.     if (ilow < 0)
  274.         ilow = 0;
  275.     if (ihigh > a->ob_size)
  276.         ihigh = a->ob_size;
  277.     if (ihigh < ilow)
  278.         ihigh = ilow;
  279.     if (ilow == 0 && ihigh == a->ob_size) {
  280.         /* XXX can only do this if tuples are immutable! */
  281.         INCREF(a);
  282.         return (object *)a;
  283.     }
  284.     np = (tupleobject *)newtupleobject(ihigh - ilow);
  285.     if (np == NULL)
  286.         return NULL;
  287.     for (i = ilow; i < ihigh; i++) {
  288.         object *v = a->ob_item[i];
  289.         INCREF(v);
  290.         np->ob_item[i - ilow] = v;
  291.     }
  292.     return (object *)np;
  293. }
  294.  
  295. object *
  296. gettupleslice(op, i, j)
  297.     object *op;
  298.     int i, j;
  299. {
  300.     if (op == NULL || !is_tupleobject(op)) {
  301.         err_badcall();
  302.         return NULL;
  303.     }
  304.     return tupleslice((tupleobject *)op, i, j);
  305. }
  306.  
  307. static object *
  308. tupleconcat(a, bb)
  309.     register tupleobject *a;
  310.     register object *bb;
  311. {
  312.     register int size;
  313.     register int i;
  314.     tupleobject *np;
  315.     if (!is_tupleobject(bb)) {
  316.         err_badarg();
  317.         return NULL;
  318.     }
  319. #define b ((tupleobject *)bb)
  320.     size = a->ob_size + b->ob_size;
  321.     np = (tupleobject *) newtupleobject(size);
  322.     if (np == NULL) {
  323.         return NULL;
  324.     }
  325.     for (i = 0; i < a->ob_size; i++) {
  326.         object *v = a->ob_item[i];
  327.         INCREF(v);
  328.         np->ob_item[i] = v;
  329.     }
  330.     for (i = 0; i < b->ob_size; i++) {
  331.         object *v = b->ob_item[i];
  332.         INCREF(v);
  333.         np->ob_item[i + a->ob_size] = v;
  334.     }
  335.     return (object *)np;
  336. #undef b
  337. }
  338.  
  339. static object *
  340. tuplerepeat(a, n)
  341.     tupleobject *a;
  342.     int n;
  343. {
  344.     int i, j;
  345.     int size;
  346.     tupleobject *np;
  347.     object **p;
  348.     if (n < 0)
  349.         n = 0;
  350.     if (a->ob_size*n == a->ob_size) {
  351.         /* Since tuples are immutable, we can return a shared
  352.            copy in this case */
  353.         INCREF(a);
  354.         return (object *)a;
  355.     }
  356.     size = a->ob_size * n;
  357.     np = (tupleobject *) newtupleobject(size);
  358.     if (np == NULL)
  359.         return NULL;
  360.     p = np->ob_item;
  361.     for (i = 0; i < n; i++) {
  362.         for (j = 0; j < a->ob_size; j++) {
  363.             *p = a->ob_item[j];
  364.             INCREF(*p);
  365.             p++;
  366.         }
  367.     }
  368.     return (object *) np;
  369. }
  370.  
  371. static sequence_methods tuple_as_sequence = {
  372.     (inquiry)tuplelength, /*sq_length*/
  373.     (binaryfunc)tupleconcat, /*sq_concat*/
  374.     (intargfunc)tuplerepeat, /*sq_repeat*/
  375.     (intargfunc)tupleitem, /*sq_item*/
  376.     (intintargfunc)tupleslice, /*sq_slice*/
  377.     0,        /*sq_ass_item*/
  378.     0,        /*sq_ass_slice*/
  379. };
  380.  
  381. typeobject Tupletype = {
  382.     OB_HEAD_INIT(&Typetype)
  383.     0,
  384.     "tuple",
  385.     sizeof(tupleobject) - sizeof(object *),
  386.     sizeof(object *),
  387.     (destructor)tupledealloc, /*tp_dealloc*/
  388.     (printfunc)tupleprint, /*tp_print*/
  389.     0,        /*tp_getattr*/
  390.     0,        /*tp_setattr*/
  391.     (cmpfunc)tuplecompare, /*tp_compare*/
  392.     (reprfunc)tuplerepr, /*tp_repr*/
  393.     0,        /*tp_as_number*/
  394.     &tuple_as_sequence,    /*tp_as_sequence*/
  395.     0,        /*tp_as_mapping*/
  396.     (hashfunc)tuplehash, /*tp_hash*/
  397. };
  398.  
  399. /* The following function breaks the notion that tuples are immutable:
  400.    it changes the size of a tuple.  We get away with this only if there
  401.    is only one module referencing the object.  You can also think of it
  402.    as creating a new tuple object and destroying the old one, only
  403.    more efficiently.  In any case, don't use this if the tuple may
  404.    already be known to some other part of the code...
  405.    If last_is_sticky is set, the tuple will grow or shrink at the
  406.    front, otherwise it will grow or shrink at the end. */
  407.  
  408. int
  409. resizetuple(pv, newsize, last_is_sticky)
  410.     object **pv;
  411.     int newsize;
  412.     int last_is_sticky;
  413. {
  414.     register tupleobject *v;
  415.     register tupleobject *sv;
  416.     int i;
  417.     int sizediff;
  418.  
  419.     v = (tupleobject *) *pv;
  420.     if (v == NULL || !is_tupleobject(v) || v->ob_refcnt != 1) {
  421.         *pv = 0;
  422.         DECREF(v);
  423.         err_badcall();
  424.         return -1;
  425.     }
  426.     sizediff = newsize - v->ob_size;
  427.     if (sizediff == 0)
  428.         return 0;
  429.     /* XXX UNREF/NEWREF interface should be more symmetrical */
  430. #ifdef Py_REF_DEBUG
  431.     --_Py_RefTotal;
  432. #endif
  433.     UNREF(v);
  434.     if (last_is_sticky && sizediff < 0) {
  435.         /* shrinking: move entries to the front and zero moved entries */
  436.         for (i = 0; i < newsize; i++) {
  437.             XDECREF(v->ob_item[i]);
  438.             v->ob_item[i] = v->ob_item[i - sizediff];
  439.             v->ob_item[i - sizediff] = NULL;
  440.         }
  441.     }
  442.     for (i = newsize; i < v->ob_size; i++) {
  443.         XDECREF(v->ob_item[i]);
  444.         v->ob_item[i] = NULL;
  445.     }
  446.     sv = (tupleobject *)
  447.         realloc((char *)v,
  448.             sizeof(tupleobject) + newsize * sizeof(object *));
  449.     *pv = (object *) sv;
  450.     if (sv == NULL) {
  451.         DEL(v);
  452.         err_nomem();
  453.         return -1;
  454.     }
  455.     NEWREF(sv);
  456.     for (i = sv->ob_size; i < newsize; i++)
  457.         sv->ob_item[i] = NULL;
  458.     if (last_is_sticky && sizediff > 0) {
  459.         /* growing: move entries to the end and zero moved entries */
  460.         for (i = newsize - 1; i >= sizediff; i--) {
  461.             sv->ob_item[i] = sv->ob_item[i - sizediff];
  462.             sv->ob_item[i - sizediff] = NULL;
  463.         }
  464.     }
  465.     sv->ob_size = newsize;
  466.     return 0;
  467. }
  468.